package com.aismy.admin.controller;

import com.aismy.admin.service.FileService;
import com.aismy.admin.vo.Result;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author Aismy
 * @date 2025/03/14
 **/
@RestController
@RequestMapping("/api/file")
@RequiredArgsConstructor
public class FileController {
    private final FileService fileService;

    @PostMapping("/upload")
    public Result<String> uploadFile(MultipartFile file) {
        return Result.success(fileService.uploadFile(file));
    }

    /**
     * 文件下载
     * @param fileName 文件名
     * @param response HTTP响应
     */
    @GetMapping("/download/{fileName}")
    public void downloadFile(@PathVariable String fileName, HttpServletResponse response) throws IOException {
        fileService.downloadFile(fileName, response);
    }

    /**
     * 获取文件访问URL
     * @param fileName 文件名
     * @return 文件访问URL
     */
    @GetMapping("/url/{fileName}")
    public Result<String> getFileUrl(@PathVariable String fileName) {
        return Result.success(fileService.getFileUrl(fileName));
    }
}
